﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Instagram.Version2.Helper
{
    public class ApiCaller
    {
        private static readonly string _url = "http://localhost:2066";
        private static HttpClient client;

        static ApiCaller()
        {
            client = new HttpClient();
            client.BaseAddress = new Uri(_url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        async public static Task<T> GetAsync<T>(string requestUri)
        {
            var response = await client.GetAsync(requestUri);

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsAsync<T>();
            }

            return default(T);
        }

        async public static Task<bool> PostAsync(string requestUri, object objects)
        {
            return (await client.PostAsJsonAsync(requestUri, objects)).IsSuccessStatusCode;
        }

        async public static Task<T> PostAsync<T>(string requestUri, object objects)
        {
            return await (await client.PostAsJsonAsync(requestUri, objects)).Content.ReadAsAsync<T>();
        }
        
        async public static Task<bool> DeleteAsync(string requestUri)
        {
            return (await client.DeleteAsync(requestUri)).IsSuccessStatusCode;
        }

        
    }
}
